//
//  WBNetworkingManager+Extenion.swift
//  WeiBoAPP
//
//  Created by 张学飞 on 2019/1/16.
//  Copyright © 2019 zxf. All rights reserved.
//

import Foundation


// MARK: - 封装afn网络请求
extension WBNetworkManager {
    
    //加载微博数据
    func statusList (completion: @escaping (_ list:[String:AnyObject]?,_ isSuccess: Bool) ->())  {
        
        let url = ""
        let parameter = [String:AnyObject]()
        tokenRequst(URLString: url, parameters: parameter) { (data, isSucces) in
            

            //从json中h获取value字典数据，
            //如果 as? 失败，result = nil
            let result = (data as? [String:AnyObject])?["value"] as?[String: AnyObject]
            
            //假数据
            let dic:[String:AnyObject] = ["code":"成功" as AnyObject]
            
            completion(dic,true)
        }
        
     
    }
    //escaping
    func unreadCount (completion:@escaping (_ count: Int) -> ())  {
        tokenRequst(URLString: "", parameters: ["String" : "AnyObject" as AnyObject]) { (data, isSuccess) in
            
            completion(10)
        }
    }
    
    func loadAccessToken(code:String,completion:@escaping (_ json:String, _ issuccess:Bool) ->()) {
        let url = "https://api.weibo.com/oauth2/access_token"
        let params = ["client_id":WB_APP_KEY,
                      "client_secret":WB_APP_SECRE,
                      "grant_type":"authorization_code",
                      "code":code,
                      "redirect_uri":WB_APP_REDIRET_URL,
        ] as [String : AnyObject]
        
        request(method: .POST, URLString: url, parameters: params ) { (data, isSuccess) in
            
            completion(data as? String ?? "",isSuccess)
        }

        
    }
    
}
